package com.rdc.dictionary.util;

import android.content.Context;

import android.media.MediaPlayer;
import android.net.Uri;
import android.widget.Toast;

import com.rdc.dictionary.application.App;
import com.rdc.dictionary.bean.Word;


import java.io.IOException;
import java.io.InputStream;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 对Word的一顿操作
 *
 * 查词的工具类，内部有方法：
 * 保存words到数据库
 * 获取address地址
 * 向数据库中查找words
 */

public class WordsAction
{

	private static WordsAction wordsAction;

	private MediaPlayer mMediaPlayer = null;

	/**
	 * 私有化的构造器
	 */
	private WordsAction(Context context)
	{

	}

	/**
	 * 单例类WordsAction获取实例方法
	 *
	 * @param context 上下文
	 */
	public static WordsAction getInstance(Context context) {
		//双重效验锁，提高性能
		if (wordsAction == null) {
			synchronized (WordsAction.class) {
				if (wordsAction == null) {
					wordsAction = new WordsAction(context);
				}
			}
		}
		return wordsAction;
	}


	/**
	 * 获取网络查找单词的对应地址
	 *
	 * @param key 要查询的单词
	 * @return address 所查单词对应的http地址
	 */
		public static String getAddressForWords(String key) {
			String address_p1 = "http://dict-co.iciba.com/api/dictionary.php?w=";
			String address_p2 = key;
			String address_p3 = "&type=json&key=1F9CA812CB18FFDFC95FC17E9C57A5E1";

			return address_p1 + address_p2 + address_p3;
		}






	/**
	 * 保存words的发音MP3文件到SD卡
	 *
	 * @param word word实例
	 */
	public void saveWordMP3(Word word)
	{
		String addressE = word.getEnglishVoice();
		String addressA = word.getAmericanVoice();
		final String filePath = word.getWord();
		if (addressE != "") {

			//网络获取对应的输入流
			HttpUtil.sentHttpRequest(addressE, new HttpUtil.HttpCallBackListener() {
				@Override
				public void onFinish(InputStream inputStream) {
					FileUtil.getInstance().writeToSD(filePath, "E.mp3", inputStream);

				}

				@Override
				public void onError() {

				}
			});
		}

		if (addressA != "") {

			HttpUtil.sentHttpRequest(addressA, new HttpUtil.HttpCallBackListener() {
				@Override
				public void onFinish(InputStream inputStream) {
					FileUtil.getInstance().writeToSD(filePath, "A.mp3", inputStream);

				}

				@Override
				public void onError() {

				}
			});

		}


	}





	/**
	 * 播放本地存放的words的发音
	 *
	 * @param wordName 单词名
	 * @param ps       E 代表英式发音
	 *                 A 代表美式发音
	 * @param context  上下文
	 */
	public void playLocalMP3(String wordName, String ps, Context context)
	{
		String fileName = wordName + "/" + ps + ".mp3";

		String address = FileUtil.getInstance().getPath(fileName);


		//若正在播放先释放后重播
		if (mMediaPlayer != null  )
		{
			if (mMediaPlayer.isPlaying())
			{
				mMediaPlayer.stop();
			}
			mMediaPlayer.release();
			mMediaPlayer = null;
		}

		//新建MediaPlayer Uri.parse有问题???
		//		mMediaPlayer = MediaPlayer.create(context, Uri.parse(address));
		//		mMediaPlayer.start();

		mMediaPlayer = new MediaPlayer();
		Uri uri=Uri.parse(address);
		try
		{
			mMediaPlayer.setDataSource(context,uri);
			mMediaPlayer.prepare();

		} catch (IOException e)
		{
			e.printStackTrace();
		}

		mMediaPlayer.start();
	}


	/**
	 * 网络播放words的发音
	 *
	 * @param address  mp3网络地址
	 *
	 * @param context  上下文
	 */
	public void playOnlineMP3(String address, Context context)
	{
		//若正在播放先释放后重播
		if (mMediaPlayer != null  )
		{
			if (mMediaPlayer.isPlaying())
			{
				mMediaPlayer.stop();
			}
			mMediaPlayer.release();
			mMediaPlayer = null;
		}

		//新建MediaPlayer Uri.parse有问题???
		//		mMediaPlayer = MediaPlayer.create(context, Uri.parse(address));
		//		mMediaPlayer.start();

		mMediaPlayer = new MediaPlayer();
		Uri uri=Uri.parse(address);
		try
		{
			mMediaPlayer.setDataSource(context,uri);
			mMediaPlayer.prepare();

		} catch (IOException e)
		{
			e.printStackTrace();
		}

		mMediaPlayer.start();
	}


}
